<?php
    //  收货地址后台数据处理页面
    require_once "weixin_info.php";//å¾®ä¿¡æŽˆæƒèŽ·å–ç”¨æˆ·å¤´åƒç­‰ä¿¡æ?
    require_once "config.php";
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $mold = empty($_POST['mold']) ? '' : $_POST['mold'];
        $id = $_POST['id'];
        if ($mold == 'delete') {
            //地址删除
            if(!empty($id)){
                $address_sql = $mysql->query("select * from dyspw_mem_address where id=".$id);
                $address_info = $address_sql->fetch_all(MYSQLI_ASSOC);
                if($address_info){
                    $address_del = $mysql->query("DELETE FROM dyspw_mem_address where id = ".$id);
                    if($address_del){
                        $result['status'] = 'y';
                        $result['info'] = '收货地址删除成功，即将刷新';
                        exit(json_encode($result));
                    }else{
                        $result['status'] = 'n';
                        $result['info'] = '收货地址删除失败';
                        exit(json_encode($result));
                    }
                }else{
                    $result['status'] = 'n';
                    $result['info'] = '未查询到该地址信息';
                    exit(json_encode($result));
                }
            }else{
                $result['status'] = 'n';
                $result['info'] = '未获取到该收货地址id';
                exit(json_encode($result));
            }

        }else{
            $params = array(
                'uid'       => $_SESSION['Member_info'][0]['id'],
                'add_place' => $_POST['add_place'],
                'add_brand' => $_POST['add_brand'],
                'add_link' => $_POST['add_link'],
                'add_sex' => $_POST['add_sex'],
                'add_mobile' => $_POST['add_mobile'],
                'add_lat' => $_POST['add_lat'],//前端根据收货地址调用地址获取收货地址经纬度，字符串拼接提交
                'add_lng' => $_POST['add_lng'],
            );
            // $sql = $mysql->query("select * from dyspw_mem_address where  id = {$id} ");
            // $address_list = $sql->fetch_all(MYSQLI_ASSOC);
            $time = time();
            if(empty($id)){
                //收货地址添加
                $address_up = $mysql->query("insert into dyspw_mem_address (uid,add_place,add_brand,add_link,add_sex,add_mobile,add_lat,add_lng, time) values ('{$params['uid']}','{$params['add_place']}','{$params['add_brand']}','{$params['add_link']}','{$params['add_sex']}','{$params['add_mobile']}','{$params['add_lat']}','{$params['add_lat']}','{$time}')");
            }else {
                //收货地址编辑
                $address_up = $mysql->query("UPDATE dyspw_mem_address SET add_place='{$params['add_place']}', add_brand='{$params['add_brand']}', add_link='{$params['add_link']}',add_sex='{$params['add_sex']}',add_lat='{$params['add_lat']}',add_lng='{$params['add_lng']}', add_mobile='{$params['add_mobile']}' where id={$id}");
            }
            if($address_up){
                $result['status'] = 'y';
                $result['info'] = '编辑成功,即将跳转';
                exit(json_encode($result));
            }else{
                $result['status'] = 'n';
                $result['info'] = '编辑失败，请重新提交';
                exit(json_encode($result));
            }
        }
    }
 ?>
